﻿@{
    ViewData["Title"] = "仓库管理";
}
<script type="text/javascript" src="~/lib/qui/thirdparty/lodop/LodopFuncs.js"></script>
<style>
    .titleName {
        font-size: 14px;
        font-weight: bold;
    }
</style>
<div class="box2" panelTitle="检索条件">
    <form id="queryForm" method="post">
        <table formMode="transparent">
            <tr>
                <td>&nbsp;&nbsp;订单号：</td>
                <td><input type="text" name="orderNo" id="orderNo" /></td>
                <td>&nbsp;&nbsp;单据类型：</td>
                <td>
                    <select prompt="全部" url="/wms/PurchaseManage/GetOrderType" name="orderType" id="orderType">
                    </select>
                </td>
                <td>&nbsp;&nbsp;单据状态：</td>
                <td>
                    <select prompt="全部" url="/wms/PurchaseManage/GetOrderStatus" name="orderStatus" id="orderStatus">
                    </select>
                </td>
                <td>
                    &nbsp;&nbsp;创建时间：
                </td>
                <td>
                    <input type="text" class="date" name="startTime" style="width:auto" id="startTime" dateFmt="yyyy-MM-dd HH:mm:ss" />
                </td>
                <td>
                    &nbsp;&nbsp;至：
                </td>
                <td>
                    <input type="text" class="date" name="endTime" style="width:auto" id="endTime" dateFmt="yyyy-MM-dd HH:mm:ss" />
                </td>
                <td>是否启用：</td>
                <td>
                    <select name="enabled" id="enabled" selectedValue="Y" data='{"list":[{"value":"","key":"全部"},{"value":"Y","key":"是"},{"value":"N","key":"否"}]}'></select>
                </td>
                <td><button type="button" onclick="searchHandler()"><span class="icon_find">查询</span></button></td>
                <td><button type="button" onclick="resetSearch()"><span class="icon_reload">重置</span></button></td>
            </tr>
        </table>
    </form>
</div>

<div class="padding_right5">
    <div class="basicTab" id="myTab">
        <div name="采购单">
            <div id="maingrid"></div>
        </div>
        <div name="采购单明细">
            <div id="maingrid1">
            </div>
        </div>
        <div name="条码明细">
            <div id="maingrid2"></div>
        </div>
    </div>
</div>

<div id="lableB" style="display:none">
    <table style="width:354px; height:262.6px;font-size:12px;font-weight:800;margin-left:15px; border-collapse: collapse;border-spacing: 0; " border="1" ;>
        <tr>
            <td colspan="8" style="height:40px">
                <label id="cartonNo1" style="width:198px"></label>
            </td>
        </tr>
        <tr style="height:40px">
            <td rowspan="2" colspan="3">
                <p class="titleName">简码：</p>
                <p><label id="partSpec" style="font-size:35px"></label></p>
            </td>
            <td rowspan="2" colspan="3">
                <p class="titleName">数量：</p>
                <p><label id="qty" style="font-size:35px"></label><label id="unit" style="font-size:35px"></label></p>
            </td>
            <td colspan="2">
                <p class="titleName">采购日期：</p>
                <p><label id="seDate"></label></p>
            </td>
        </tr>

        <tr>
            <td colspan="2" style="height:20px">
                <p class="titleName">采购时间：</p>
                <p><label id="seTime"></label></p>
            </td>
        </tr>
        <tr>
            <td colspan="6" style="height:20px">
                <p class="titleName">物料编码：<label id="partNo">11</label></p>
            </td>
            <td colspan="2" style="height:20px">
                <p class="titleName">供应商代码：</p>
                <p><label id="vendorCode"></label></p>
            </td>
        </tr>
        <tr>
            <td colspan="6" style="height:20px">
                <p class="titleName">供应商名称：</p>
                <p><label id="vendorName"></label></p>
            </td>
            <td colspan="2" style="height:20px">
                <p class="titleName">客户：</p>
                <p><label id="customerCode"></label></p>
            </td>
        </tr>
        <tr>
            <td colspan="8" style="height:20px">
                <p class="titleName">物料描述：</p>
                <p><label id="partSpec2"></label></p>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">
    var grid; var grid1; var grid2;
    function initComplete() {
        grid = $("#maingrid").quiGrid({
            columns: [
                { display: '订单号', name: 'orderNo', align: 'center', width: 200 },
                { display: '单据类型', name: 'orderTypeDesc', align: 'center', width: 120 },
                { display: '单据状态', name: 'orderStatusDesc', align: 'center', width: 140 },
                { display: '供应商代码', name: 'vendorCode', align: 'center', width: 160 },
                { display: '供应商名称', name: 'vendorName', align: 'center', width: 180 },
                { display: '采购日期', name: 'seDate', align: 'center', width: 140 },
                { display: '采购时间', name: 'seTime', align: 'center', width: 140 },
                { display: '道口号', name: 'dock', align: 'center', width: 120 },
                { display: '工厂代码', name: 'factoryCode', align: 'center', width: 130 },
                { display: '创建时间', name: 'createTime', align: 'center', width: 230 },
                {
                    display: '启用/禁用', name: 'enabled', align: 'center', width: "200%",
                    render: function(rowdata, rowindex, value, column) {
                        if (value == "Y") {
                            return "启用";
                        } else {
                            return "禁用";
                        }
                    }
                }
            ],
            url: '/wms/PurchaseManage/getPurchaseListByPage', sortName: 'orderNo', rownumbers: true, checkbox: true,
            height: "100%", width: "100%", percentWidthMode: true, pageSize: 100, usePager: true,
            //顶部图标按钮栏rownumbers
            toolbar: {}
        });

        grid1 = $("#maingrid1").quiGrid({
            columns: [
                { display: '订单号', name: 'orderNo', align: 'center', width: "7%" },
                { display: '项目号', name: 'itemNo', align: 'center', width: "5%" },
                { display: '零件号', name: 'partNo', align: 'center', width: "15%" },
                { display: '零件名称', name: 'partSpec', align: 'center', width: "7%" },
                { display: '数量', name: 'qty', align: 'center', width: "5%" },
                { display: '已收数量', name: 'recQty', align: 'center', width: "7%" },
                { display: '包装数量', name: 'boxQty', align: 'center', width: "7%" },
                { display: '已收包装数量', name: 'recBoxQty', align: 'center', width: "7%" },
                { display: '标准数量', name: 'snpQty', align: 'center', width: "7%" },
                { display: '单位', name: 'unit', align: 'center', width: "5%" },
                { display: '库存地', name: 'erpWarehouse', align: 'center', width: "7%" },
                { display: '状态', name: 'itemStatusDesc', align: 'center', width: "7%" },
                { display: '工厂代码', name: 'factoryCode', align: 'center', width: "7%" },
                { display: '启用/禁用', name: 'enabled', align: 'center', width: "5%" }
            ],
            data: [], sortName: 'orderNo', rownumbers: true, checkbox: true,
            height: "100%", width: "100%", percentWidthMode: true, pageSize: 100, usePager: true
        });
        grid2 = $("#maingrid2").quiGrid({
            columns: [
                { display: '单号', name: 'refOrderNo', align: 'center', width: 200 },
                { display: '条码', name: 'cartonNo', align: 'center', width: 260 },
                { display: '零件号', name: 'partNo', align: 'center', width: 260 },
                { display: '零件号简码', name: 'partSpec', align: 'center', width: 100 },
                { display: '零件号名称', name: 'partSpec2', align: 'center', width: 240 },
                { display: '单位', name: 'unit', align: 'center', width: 100 },
                { display: '数量', name: 'qty', align: 'center', width: 100 },
                { display: '状态', name: 'stockStatus', align: 'center', width: 100 },
                { display: '收货时间', name: 'reveiceTime', align: 'center', width: 160 },
                { display: '工厂代码', name: 'factoryCode', align: 'center', width: 100 },
                {
                    display: '启用/禁用', name: 'enabled', align: 'center', width: "200%",
                    render: function(rowdata, rowindex, value, column) {
                        if (value == "Y") {
                            return "启用";
                        } else {
                            return "禁用";
                        }
                    }
                },
            ],
            data: [], sortName: 'cartonNo', rownumbers: true, checkbox: true,
            height: "100%", width: "100%", percentWidthMode: true, pageSize: 100, usePager: true
        });
        //载入工具栏图标
        loadToolbar("", grid);
        addDoubleClick("", grid);

        addDoubleClick("", grid1);

        $("#myTab").bind("actived", function(e, idx) {
            if (idx == 0) {
                grid.resetHeight();
                grid.resetWidth();
            }
            else if (idx == 1) {
                grid1.resetHeight();
                grid1.resetWidth();
            }
            else if (idx == 2) {
                grid2.resetHeight();
                grid2.resetWidth();
            }
        })
    }

    //查询
    function searchHandler() {
        //得到查询参数
        var query = $("#queryForm").formToArray();

        grid1.setOptions({ params: query });
        grid1.setNewPage(1);
        grid1.loadData();

        //将查询参数传给grid表格
        grid.setOptions({ params: query });
        //页号重置为1
        grid.setNewPage(1);
        //重新加载数据
        grid.loadData();

        $.post("/wms/PurchaseManage/getPurchaseDetailListByPage",
            {
                "orderNo": $("#orderNo").val()
            },
            function(result) {
                grid1.loadData(result);
            }, "json");
    }

    //重置查询
    function resetSearch() {
        $("#queryForm")[0].reset();
        searchHandler();
    }

    //双击
    function onDblClickRow(rowdata, rowid, rowobj) {
        var idxs = $("#myTab").attr("selectedIdx");
        if (idxs == 0) {
            $.post("/wms/PurchaseManage/getPurchaseDetailListByPage",
                {
                    "orderNo": rowdata.orderNo
                },
                function(result) {
                    grid1.loadData(result);
                }, "json");

            $("#myTab").basicTabSetIdx(1);
        }
        else if (idxs == 1) {
            $.post("/wms/PurchaseManage/getStockListByPage",
                {
                    "orderNo": rowdata.orderNo,
                    "partNo": rowdata.partNo,
                },
                function(result) {
                    grid2.loadData(result);
                }, "json");

            $("#myTab").basicTabSetIdx(2);
        }

    }

    //添加
    function onAdd() {
        top.Dialog.open({ URL: "/wms/PurchaseManage/EditPurchaseManage", Title: "新增", Width: 700, Height: 600 });
    }

    //打印
    function print() {
        var gridrows = grid.getSelectedRows();
        var gridrowsLength = gridrows.length;
        if (gridrowsLength == 0) {
            top.Dialog.alert("请选中要打印的单据!");
            return;
        }
        if (gridrowsLength > 1) {
            top.Dialog.alert("请选择一条要打印的单据!");
            return;
        }
        if (gridrows[0].orderStatus == 10) {
            top.Dialog.alert("未生成条码，请先生成条码！");
            return;
        }

        //当第三方api提供的是异步方法时 这里用setTimeout模拟
        $.post("/wms/PurchaseManage/getStockListByPrint",
            {
                "orderNo": gridrows[0].orderNo
            },
            function(result) {
                var selectedRows = result
                var selectedRowsLength = selectedRows.length;
                if (selectedRowsLength == 0) {
                    top.Dialog.alert("未查到条码数据，请检查!");
                    return;
                }
                LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
                LODOP.SET_PRINT_PAGESIZE(1, 1000, 700, "");
                for (var i = 0; i < selectedRowsLength; i++) {
                    LODOP.NewPage();
                    $("#qty").text(selectedRows[i].qty);
                    $("#unit").text(selectedRows[i].unit);
                    $("#partSpec").text(selectedRows[i].partSpec);
                    $("#seDate").text(selectedRows[i].seDate);
                    $("#seTime").text(selectedRows[i].seTime);
                    $("#partNo").text(selectedRows[i].partNo);
                    $("#vendorCode").text(selectedRows[i].vendorCode);
                    $("#vendorName").text(selectedRows[i].vendorName);
                    $("#customerCode").text("");
                    $("#partSpec2").text(selectedRows[i].partSpec2);

                    LODOP.ADD_PRINT_TABLE(0, 0, 368, 264.6, document.getElementById("lableB").innerHTML);
                    LODOP.ADD_PRINT_BARCODE(5, 30, 320, 30, "128Auto", selectedRows[i].cartonNo);
                    //LODOP.SET_PRINT_STYLEA(0, "FontName", "39Extended");
                }
                //LODOP.PRINT_DESIGN();
                LODOP.PREVIEW();
                //LODOP.PRINT();

            }, "json");
    }

    //删除后的提示
    function handleResult(result) {
        if (result != -1) {
            top.Dialog.alert("删除成功！删除" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("删除失败！");
        }
    }
    //生成条码
    function onBarcodeGenerator() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选择采购单!");
            return;
        }

        if (rows[0].orderType != "10") {//PO
            top.Dialog.alert("该单据类型不是PO采购单不允许生成");
            return;
        }
        if (rows[0].enabled != "Y") {
            top.Dialog.alert("该单据已禁用");
            return;
        }
        top.Dialog.confirm("是否确认生成条码？", function() {
            $.post("/wms/PurchaseManage/onBarcodeGenerator",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    if (result.flag == "OK") {
                        top.Dialog.alert(result.message);
                        grid.loadData();
                    }
                    else {
                        top.Dialog.alert(result.message);
                    }
                },
                "json");
        });
    }


    //获取所有选中行获取选中行的id 格式为 ids=1&ids=2
    function getSelectId(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            ids += selectedRows[i].ruid + ",";
        }
        return { "ids": ids };
    }


    function getSelectOrderNo(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            ids += "'" + selectedRows[i].orderNo + "',";
        }
        return { "orderNos": ids };
    }
    //禁用
    function ClosehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("单据关闭成功！关闭" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("单据关闭失败！");
        }
    }

    //关闭
    function onClose() {
        $.post("/wms/PurchaseManage/onClose",
            //获取所有选中行
            getSelectOrderNo(grid),
            function(result) {
                ClosehandleResult(result.status);
            },
            "json");
    }

    //启用
    function EnablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("启用成功！启用" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("启用失败！");
        }
    }

    //禁用
    function DisablehandleResult(result) {
        if (result != -1) {
            top.Dialog.alert("禁用成功！禁用" + result + "条数据", null, null, null, 1);
            grid.loadData();
        } else {
            top.Dialog.alert("禁用失败！");
        }
    }
    //启用
    function onEnable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要启用的记录!");
            return;
        }
        $.post("/wms/PurchaseManage/onEnable",
            //获取所有选中行
            getSelectOrderNo(grid),
            function(result) {
                EnablehandleResult(result.status);
            },
            "json");
    }
    //禁用
    function onDisable(rowid, rowidx) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要禁用的记录!");
            return;
        }
        top.Dialog.confirm("确定要禁用吗？", function() {
            $.post("/wms/PurchaseManage/onDisable",
                //获取所有选中行
                getSelectOrderNo(grid),
                function(result) {
                    DisablehandleResult(result.status);
                },
                "json");
        });
    }


    //刷新表格 表单提交的回调
    function afterFormSubmit() {
        grid.loadData();

        grid1.loadData();

        grid2.loadData();
    }

    //刷新表格数据并重置排序和页数
    function refresh(isUpdate) {
        if (!isUpdate) {
            //重置排序
            grid.options.sortName = 'orderNo';
            grid.options.sortOrder = "desc";
            //页号重置为1
            grid.setNewPage(1);

            grid1.options.sortName = 'orderNo';
            grid1.options.sortOrder = "desc";
            //页号重置为1
            grid1.setNewPage(1);

            grid2.options.sortName = 'cartonNo';
            grid2.options.sortOrder = "desc";
            //页号重置为1
            grid2.setNewPage(1);
        }
        grid.loadData();
        grid1.loadData();
        grid2.loadData();
    }

</script>	